#include "contacts.pb.h"
#include<iostream>
#include<fstream>
using namespace std;


void PrintContacts(const contacts1::Contacts& contacts){
    for(int i=0;i<contacts.people_size();i++){
        const contacts1::PeopleInfo& peopleinfo=contacts.people(i);    //通过下标获取people
        std::cout<<"----------联系人"<<i+1<<"----------"<<std::endl;
        std::cout<<"联系人姓名： "<<peopleinfo.name()<<std::endl;
        std::cout<<"联系人年龄： "<<peopleinfo.age()<<std::endl;
        int j=1;
        for(;j<=peopleinfo.phone_size();j++){
            std::cout<<"电话： "<<peopleinfo.phone(j-1).number();
            contacts1::Phone phone=peopleinfo.phone(j-1);
            std::cout<<"  类型["<<phone.type()<<"]"<<std::endl;

        }
        //如果有address成员，并且address成员的类型是Address
        if(peopleinfo.has_address()&&peopleinfo.address().Is<contacts1::Address>()){
            contacts1::Address address;
            peopleinfo.address().UnpackTo(&address);
            if(!address.home_address().empty()){
                std::cout<<"家庭地址："<<address.home_address()<<std::endl;
            }

            if(!address.unit_address().empty()){
                std::cout<<"单位地址："<<address.unit_address()<<std::endl;
            }
        }

        /* 
            第一种写法
        if (people.has_qq()) {
        
        } else if (people.has_weixin()) {
        
        } */

        //写法二
        switch (peopleinfo.gender_case())
        {
        case contacts1::PeopleInfo::kMale:
            std::cout<<"性别：male"<<std::endl;
            break;
        case contacts1::PeopleInfo::kFemale:
            std::cout<<"性别：female"<<std::endl;
            break;
        case contacts1::PeopleInfo::GENDER_NOT_SET:
            std::cout<<"性别：未设置"<<std::endl;
            break;
        default:
            std::cout<<"未知错误! "<<std::endl;
            break;
        }

        //打印备注
        if(peopleinfo.remarks_size()){
            for(auto it=peopleinfo.remarks().cbegin();it!=peopleinfo.remarks().end();it++){
                std::cout << " " << it->first << ": " << it->second << std::endl;
            }
        }
    }
}

int main(){
    contacts1::Contacts contacts;
    fstream input("contacts.bin",ios::binary | ios::in);
    if(!input){
        std::cout << " contacts.bin not find,create new file! " << std::endl;
    }
    else if(!contacts.ParseFromIstream(&input)){

        std::cerr<<" Failed to Parse contacts.bin !"<<std::endl;
        input.close();
        return -1;
    }
    PrintContacts(contacts);
    input.close();
    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}
